import request from '../../utils/request'
import {
  getLocalChannels,
  hasToken,
  setLocalChannels,
} from '../../utils/storage'
import {
  SAVE_ALLCHANNELS,
  SAVE_ARTICLE_LIST,
  SAVE_CHANNELS,
} from '../action_type/home'
// import SAVE_CHANNELS from '../action_type/home' //注意这里要大括号
/**
 * 获取用户的频道
 * @returns
 */
export const getUserChannels = () => {
  return async (dispatch) => {
    //   1 判断用户是否登录
    if (hasToken()) {
      //   console.log(1)
      const res = await request.get('/user/channels')
      dispatch(seveUserChannels(res.data.data.channels))
    } else {
      // 2如果没有token 从本地频道获取数据
      const channels = getLocalChannels()
      if (channels) {
        //   没有token 但本地有channnels数据
        dispatch(seveUserChannels(channels))
      } else {
        //   没有token 且本地没有channels数据
        const res = await request.get('/user/channels')
        dispatch(seveUserChannels(res.data.data.channels))
        // 保存到本地
        setLocalChannels(res.data.data.channels)
      }
    }
    // console.log(res)
    // console.log(res.data.data.channels)
  }
}
// 保存用户频道到redux中
export const seveUserChannels = (payload) => {
  return {
    type: SAVE_CHANNELS,
    payload,
  }
}
// 获取所有频道
export const getAllChannels = () => {
  return async (dispatch) => {
    const res = await request.get('/channels')
    // console.log(res)
    dispatch(seveAllChannels(res.data.data.channels))
    // console.log(res.data.data.channels)
  }
}
// 保存所有频道
export const seveAllChannels = (payload) => {
  return {
    type: SAVE_ALLCHANNELS,
    payload,
  }
}

// 删除频道

export const delChannel = (channel) => {
  return async (dispatch, getState) => {
    // 如果用户登录了 ，需要发送请求删除频道
    // 用户没有登录，删除本地频道
    // 不论是否登录，都需要redux中的频道
    const userChannels = getState().home.userChannels
    if (hasToken()) {
      // 发送请求
      await request.delete('/user/channels/' + channel.id)
      // t同步频道的数据到redux
      // console.log(res)
      // dispatch()
      dispatch(
        seveUserChannels(userChannels.filter((item) => item.id !== channel.id))
      )
    } else {
      // 没有登录 修改本地 修改redux
      const result = userChannels.filter((item) => item.id !== channel.id)
      dispatch(seveUserChannels(result))
      setLocalChannels(result) //重新存入本地
    }
  }
}

// 添加频道
export const addChannel = (channel) => {
  return async (dispatch, getState) => {
    const channels = [...getState().home.userChannels, channel]
    if (hasToken()) {
      // 发请求添加
      await request.patch('/user/channels', {
        channels: [channel],
      })
      // 加入redux
      dispatch(seveUserChannels(channels))
    } else {
      dispatch(seveUserChannels(channels))
      setLocalChannels(channels)
    }
  }
}

// 获取文章列表数据
export const getArticleList = (channelId, timestamp, loadMore = false) => {
  return async (dispatch) => {
    const res = await request({
      url: '/articles',
      method: 'get',
      params: {
        channel_id: channelId,
        timestamp: timestamp,
      },
    })
    // console.log(res)

    dispatch(
      setArticleList({
        channelId,
        timestamp: res.data.data.pre_timestamp,
        list: res.data.data.results,
        loadMore,
      })
    )
  }
}

export const setArticleList = (payload) => {
  return {
    type: SAVE_ARTICLE_LIST,
    payload,
  }
}
